//
//  NoticeViewController.m
//  cm
//
//  Created by suyibaishan on 2017/10/31.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "NoticeViewController.h"
#import "NoticeDetailViewController.h"

@interface NoticeViewController () <UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSMutableArray *dataSource;
     NSInteger page;
}
@end

@implementation NoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createNoticeView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getAnnouncementList:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] pageNum:1 pageSize:16 url:MAnnouncementList completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            [dataSource removeAllObjects];
            [dataSource addObjectsFromArray:responseJson[@"data"][@"list"]];
            [_tableView reloadData];
            page = 2;
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

-(void)loadMoreData
{
    [[ToolNet sharedManager] getAnnouncementList:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] pageNum:page pageSize:16 url:MAnnouncementList completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            if ([responseJson[@"data"][@"pages"] integerValue] >= page) {
                [dataSource addObjectsFromArray:responseJson[@"data"][@"list"]];
                [_tableView.mj_footer endRefreshing];
                page++;
            }
            else{
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [_tableView reloadData];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
            [_tableView.mj_footer endRefreshing];
        }
    }];
}

-(void)createNoticeView
{
    self.titleLabel.text = @"车盟公告";
    self.backButton.hidden = NO;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData];
    }];
    _tableView.mj_header.automaticallyChangeAlpha = YES;
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
    
    dataSource = [NSMutableArray array];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    cell.textLabel.text = dataSource[indexPath.section][@"title"];
    cell.detailTextLabel.text = dataSource[indexPath.section][@"subtitle"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NoticeDetailViewController *noticeDetailVC = [[NoticeDetailViewController alloc] init];
    noticeDetailVC.content = dataSource[indexPath.section][@"content"];
    [self.navigationController pushViewController:noticeDetailVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor =  [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    return view;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.01)];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
